﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Collections;

namespace WPExpenseReportDemo
{
    public partial class ReportDetail : PhoneApplicationPage
    {
        public ReportDetail()
        {
            InitializeComponent();

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            string s = this.NavigationContext.QueryString["reportName"].ToString();
            string reportID = s.Split(':')[0].Trim();

            XDocument docReceipts = PageMain.doc;

            //find the report
            var report = from r in docReceipts.Descendants("Report")
                         where r.Attributes("id").First().Value.Equals(reportID)
                         select r;

            //select all receipts
            var receipts = from rec in report.Descendants("Receipt")
                                   select new Receipt 
                                   {
                                        ReceiptID = rec.Attributes("id").First().Value,
                                        ReceiptName = rec.Attributes("name").First().Value,
                                        ReceiptDateTime = rec.Attributes("receiptDate").First().Value,
                                        ReceiptCategory = rec.Attributes("category").First().Value,
                                        ReceiptImage = rec.FirstNode.ToString()
                                   };

            this.lbReceipts.ItemsSource = receipts;

        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            this.lbReceipts.ItemTemplate = (DataTemplate)this.Resources["lbReceiptsDelete"];
        }

    }
}